using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using E4RPDevExpress10;
using E4RPTools;
using MobileLoader.Controls;

namespace MobileLoader
{
    public partial class MainRibbonForm : RibbonForm
    {
        private Controller _controller;

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.OwnerWindow = this;
                tmnLoadsXtraUserControl.Controller = _controller;
                tmnBillsXtraUserControl.Controller = _controller;
                tmnMovementsXtraUserControl.Controller = _controller;
                tmnDocumentsXtraUserControl.Controller = _controller;
                _controller.ImportingMessage += ControllerImportingMessage;
            }
        }


        public MainRibbonForm()
        {
            InitializeComponent();
            // Import GRoup
            importTmnLoadsBarButtonItem.ItemClick += ImportTmnLoadsBarButtonItemItemClick;
            deleteTmnLoadsBarButtonItem.ItemClick += DeleteTmnLoadsBarButtonItemItemClick;
            //
            importTMNBillsBarButtonItem.ItemClick += ImportTmnBillsBarButtonItemItemClick;
            deleteTmnBillsBarButtonItem.ItemClick += DeleteTmnBillsBarButtonItemItemClick;
            //
            refreshBarButtonItem.ItemClick += RefreshBarButtonItemItemClick;
            exportExcelBarButtonItem.ItemClick += ExportExcelBarButtonItemItemClick;
            // Reports
            previewTMNListBarButtonItem.ItemClick += PreviewTmnListBarButtonItemItemClick;
            printTMNListBarButtonItem.ItemClick += PrintTmnListBarButtonItemItemClick;
            // About Group
            aboutBarButtonItem.ItemClick += AboutBarButtonItemItemClick;
            exitBarButtonItem.ItemClick += ExitBarButtonItemItemClick;
            Closing += MainRibbonFormClosing;
        }
      
        private static string GetFileName()
        {
            var openFileDialog = new OpenFileDialog
            {
                FilterIndex = 1,
                RestoreDirectory = true,
                AddExtension = true,
                CheckFileExists = true,
                CheckPathExists = true,                
                DefaultExt = ".xls"
            };
            return openFileDialog.ShowDialog(null) != DialogResult.OK ? "" : openFileDialog.FileName;
        }


        void ControllerImportingMessage(object sender, ImportingArgs e)
        {
            importingBarStaticItem.Caption = e.Message;
        }

        void ImportTmnLoadsBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var fileName = GetFileName();
            if (fileName == "")
                return;
            _controller.ImportTmnLoads(fileName);
            xtraTabControl.SelectedTabPage = tmnLoadsXtraTabPage;
        }
        
        void DeleteTmnLoadsBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var form = new DeleteDayXtraForm();
            form.ShowDialog();
            if(form.DialogResult != DialogResult.OK)
                return;
            _controller.DeleteTmnLoads(form.Day);
            xtraTabControl.SelectedTabPage = tmnLoadsXtraTabPage;
        }   

        void ImportTmnBillsBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var fileName = GetFileName();
            if (fileName == "")
                return;
            _controller.ImportTmnBills(fileName);
            xtraTabControl.SelectedTabPage = tmnBillsXtraTabPage;
        }

        void DeleteTmnBillsBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
             var form = new DeleteDayXtraForm();
            form.ShowDialog();
            if(form.DialogResult != DialogResult.OK)
                return;
            _controller.DeleteTmnBills(form.Day);
            xtraTabControl.SelectedTabPage = tmnBillsXtraTabPage;
        }

        void RefreshBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IListRefresh)control).RefreshList();
        }

        void ExportExcelBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            DevExpressHelper.ExportToExcel(((IGridView)control).GridView());
        }

        void PrintTmnListBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var filter = new PrintListFilterXtraForm {ShopData = _controller.GetShopsOfLoadsAndBills()};
            filter.ShowDialog();
            if (filter.DialogResult == DialogResult.Cancel)
                return;
            _controller.PrintTmnList(filter.Date, filter.Shop);
        }

        void PreviewTmnListBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var filter = new PrintListFilterXtraForm {ShopData = _controller.GetShopsOfLoadsAndBills()};
            filter.ShowDialog();
            if(filter.DialogResult == DialogResult.Cancel)
                return;
            _controller.PreviewTmnList(filter.Date, filter.Shop);
        }


        static void AboutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var product = new AboutLabelControl("Mobile Loader", new Font("Jokerman", 22F, FontStyle.Regular, GraphicsUnit.Point, 0), Color.Chocolate, new Point(127, 38));
            var form = new AboutXtraForm { Version = "1.0.0.0", Product = product };
            form.ShowDialog();
        }

        void ExitBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.CloseConnections();
            Close();
        }

        void MainRibbonFormClosing(object sender, CancelEventArgs e)
        {
            _controller.CloseConnections();
        }

    }
}